package com.zeng.jiudian.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.NewModel;
import com.zeng.jiudian.service.NewService;
import com.zeng.jiudian.service.impl.NewServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author JJ_yo
 * @Date 2020/7/27 - 14:50
 */
@WebServlet("/back/new/add")
public class NewAddServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String title = req.getParameter("title");
        String from = req.getParameter("from");
        String author = req.getParameter("author");
        String type = req.getParameter("type");
        String isShow = req.getParameter("isShow");
        String recommend = req.getParameter("recommend");
        String content = req.getParameter("content");
        String introduction = req.getParameter("introduction");
        String newType = req.getParameter("newType");

        NewModel newModel = new NewModel();
        newModel.setTitle(title);
        newModel.setIsFrom(from);
        newModel.setAuthor(author);
        newModel.setType(type);
        newModel.setIsShow(isShow == null ? 0 : Integer.parseInt(isShow));
        newModel.setRecommend(recommend == null ? 0 : Integer.parseInt(recommend));
        newModel.setContent(content);
        newModel.setIntroduction(introduction);
        newModel.setNewTypeId(newType == null ? null : Integer.parseInt(newType));

        NewService newService = new NewServiceImpl();
        int count = newService.add(newModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
